package com.base.library.manager;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmModel;
import io.realm.RealmQuery;
import io.realm.RealmResults;

public class RealmManager {

    private Realm realm;

    public RealmManager(Realm realm) {
        this.realm = realm;
    }

    public Realm getRealm() {
        return realm;
    }

    public void close() {
        try {
            realm.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加(性能优于下面的saveOrUpdate（）方法)
     *
     * @param object
     * @return 保存或者修改是否成功
     */
    public boolean insert(RealmModel object) {
        try {
            realm.beginTransaction();
            realm.insert(object);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }

    /**
     * 添加(性能优于下面的saveOrUpdateBatch（）方法)
     *
     * @param list
     * @return 批量保存是否成功
     */
    public boolean insert(RealmList<? extends RealmModel> list) {
        try {
            realm.beginTransaction();
            realm.insert(list);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }

    /**
     * 添加或者修改(性能优于下面的saveOrUpdate（）方法)
     *
     * @param object
     * @return 保存或者修改是否成功
     */
    public boolean insertOrUpdate(RealmModel object) {
        try {
            realm.beginTransaction();
            realm.insertOrUpdate(object);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }

    /**
     * 添加或者修改(性能优于下面的saveOrUpdateBatch（）方法)
     *
     * @param list
     * @return 保存或者修改是否成功
     */
    public boolean insertOrUpdate(RealmList<? extends RealmModel> list) {
        try {
            realm.beginTransaction();
            realm.insertOrUpdate(list);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }

    /**
     * 添加或者修改
     *
     * @param object
     * @return 已经保存的对象
     */
    public <E extends RealmModel> E copyToRealm(E object) {
        E savedE = null;
        try {
            realm.beginTransaction();
            savedE = realm.copyToRealm(object);
            realm.commitTransaction();
            return savedE;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return savedE;
        }
    }

    /**
     * 批量添加或者修改
     *
     * @param list
     * @return 全部保存成功 或 全部失败
     */
    public boolean copyToRealm(RealmList<? extends RealmModel> list) {
        try {
            realm.beginTransaction();
            realm.copyToRealm(list);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }

    /**
     * 添加或者修改
     *
     * @param object
     * @return 已经保存的对象
     */
    public <E extends RealmModel> E copyToRealmOrUpdate(E object) {
        E savedE = null;
        try {
            realm.beginTransaction();
            savedE = realm.copyToRealmOrUpdate(object);
            realm.commitTransaction();
            return savedE;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return savedE;
        }
    }

    /**
     * 批量添加或者修改
     *
     * @param list
     * @return 全部保存成功 或 全部失败
     */
    public boolean copyToRealmOrUpdate(RealmList<? extends RealmModel> list) {
        try {
            realm.beginTransaction();
            realm.copyToRealmOrUpdate(list);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }

    /**
     * save or update RealmModel from json data
     *
     * @param jsonObject json数据
     * @param clazz      具体类型
     * @return 已经保存的对象
     */
    public RealmModel insertOrUpdateFromJSON(Class<? extends RealmModel> clazz, String jsonObject) {
        RealmModel realmModel = null;
        try {
            realm.beginTransaction();
            realmModel = realm.createOrUpdateObjectFromJson(clazz, jsonObject);
            realm.commitTransaction();
            return realmModel;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return realmModel;
        }
    }

    /**
     * 删除当前表中所有数据
     *
     * @param clazz
     * @return
     */
    public boolean deleteAll(Class<? extends RealmModel> clazz) {
        try {
            realm.beginTransaction();
            realm.delete(clazz);
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }

    /**
     * 按照id删除制定的记录
     *
     * @param clazz       类型
     * @param idFieldName id字段的名称
     * @param id          id字段值
     * @return
     */
    public boolean deleteByField(Class<? extends RealmModel> clazz, String idFieldName, int id) {
        return deleteByField(clazz, idFieldName, String.valueOf(id));
    }

    /**
     * 按照id删除制定的记录
     *
     * @param clazz       类型
     * @param idFieldName id字段的名称
     * @param id          id字段值
     * @return
     */
    public boolean deleteByField(Class<? extends RealmModel> clazz, String idFieldName, String id) {
        try {
            realm.beginTransaction();
            realm.where(clazz).equalTo(idFieldName, id).findAll().deleteAllFromRealm();
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }

    public <E extends RealmModel> E copyFromRealm(E realmObject) {
        return realm.copyFromRealm(realmObject);
    }

    public <E extends RealmModel> List<E> copyFromRealm(Iterable<E> realmObjects) {
        return realm.copyFromRealm(realmObjects);
    }

    /**
     * 查询所有
     *
     * @return 返回结果集合
     */
    public RealmResults findAll(Class<? extends RealmModel> clazz) {
        return realm.where(clazz).findAll();
    }

    /**
     * 查询第一个
     *
     * @param clazz
     * @param <E>
     * @return
     */
    public <E extends RealmModel> E findFirst(Class<E> clazz) {
        return realm.where(clazz).findFirst();
    }

    public <E extends RealmModel> RealmQuery<E> where(Class<E> clazz) {
        return realm.where(clazz);
    }

    /**
     * 清空数据库
     *
     * @return
     */
    public boolean clearDatabase() {
        try {
            realm.beginTransaction();
            realm.deleteAll();
            realm.commitTransaction();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            realm.cancelTransaction();
            return false;
        }
    }
}
